<?php
namespace app\admin\controller\system;


use app\common\model\system\SystemAuth;
use app\common\controller\AdminController;
use think\facade\Session;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="角色权限管理")
 * Class Auth
 * @package app\admin\controller\system
 */
class Auth extends AdminController
{
    protected $sort = [
        'sort' => 'desc',
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $user =  Session::get('admin');
        $this->store_id = $user['store_id'];
        $this->model = app('app\common\model\system\SystemAuth');
        $this->menu = app('app\common\model\system\SystemMenu');
    }

    /**
     * 查询所属商家账号下信息
     */
    public function get_where($arr = []){

        $array['store_id'] = $this->store_id;
        $array['app_id'] = $this->model->getApp();

//        $array = array_filter($array);
        return $array;
    }
    /**
     * @NodeAnotation(title="授权")
     */
    public function authorize($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $list = $this->model->getAuthorizeNodeListByAdminId($id);
            return json($list);
        }

    }

    /**
     * 获取验证规则
     * @param $post
     * @return array
     */
    public function get_check_rule($post)
    {
        $rule = [
            'title|角色名称' => 'require|unique:system_menu,title,'.$post['id'].',id',

        ];

        return $rule;
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();

            // 启动事务
            Db::startTrans();
            try {
            $rule = [];
            if (method_exists($this, 'get_check_rule')) {
                $rule = $this->get_check_rule($post ?? []);
            }

            $this->validate($post, $rule);

            $post['app_id'] = $this->model->getApp();
            $post['store_id'] = $this->store_id;
            $save = $this->model->save($post);

            // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view();
    }
    /**
     * @NodeAnotation(title="授权保存")
     */
    public function saveAuthorize()
    {
        $id = $this->request->param('id');
        $node = $this->request->post('authIds', "[]");
        $node = json_decode($node, true);

        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        try {
            $authNode = app('app\common\model\system\SystemAuthNode');

            $authNode->where('auth_id', $id)->delete();
            if (!empty($node)) {
                $saveAll = [];
                foreach ($node as $vo) {
                    $saveAll[] = [
                        'auth_id' => $id,
                        'node_id' => $vo,
                    ];
                }
                $authNode->saveAll($saveAll);
            }

        } catch (\Exception $e) {
            $this->error('保存失败');
        }
        $this->success('保存成功');
    }

}